import { _decorator, Collider, Collider2D, Component, Contact2DType, EventTouch, Input, input, Node, Vec3 } from 'cc';
import { GameManager, GameState } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('PlayerCtrl')
export class PlayerCtrl extends Component {

    @property(Node)
    towerArray:Node[]=[];
    @property(Node)
    TowerCtrl_Btns_Player:Node=null;

    @property
    revolveSpeed:number=10;
    
    private canControl:boolean = true;

    @property
    private playerHp:number=100;

    private collider:Collider2D=null;

    private bleedTime:number=0;

    start() {
        this.collider=this.node.getComponent(Collider2D);
        this.bleeding();

    }

    update(deltaTime: number) {
        this.revolve(deltaTime);
    }

    bleeding() {
        this.collider.on(Contact2DType.BEGIN_CONTACT, (selfCollider: Collider2D, otherCollider: Collider2D) => {
            if (otherCollider.group == 4) {
                const currentTime = Date.now();
                if (currentTime - this.bleedTime >= 500) {        //每隔毫秒调用tstdgrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
                    this.bleedTime = currentTime;
                    this.playerHp -= 1;
                }
            }
            if (otherCollider.group == 8) {
                this.playerHp -= 1;
            }
            if (this, this.playerHp <= 0) {
                GameManager.getInstance().gameState === GameState.LOSE;
            }
            console.log("playerHp:", this.playerHp);
        }, this);
    }

    onTouchMove(event:EventTouch){

        if(this.canControl==false)return;
        // if(this.lifeCount<1)return;
        
        const p = this.node.position.clone();

        let targetPosition= new Vec3(p.x+event.getDeltaX(),p.y+event.getDeltaY(),p.z);
        // console.log(targetPosition.y,event.getDeltaY(),p.y);

        if(targetPosition.x<-220){
            targetPosition.x = -220;
        }
        if(targetPosition.x>220){
            targetPosition.x=220;
        }
        if(targetPosition.y>400){
            targetPosition.y = 400;
        }
        if(targetPosition.y<-400){
            targetPosition.y=-400;
        }

        this.node.setPosition(targetPosition);

    }

    revolve(deltaTime){
            const rotation=this.TowerCtrl_Btns_Player.eulerAngles;
            
            const newRotation = new Vec3;
            newRotation.z = rotation.z;

            const towerItem=new Vec3
        if (GameManager.getInstance().gameState === GameState.START) {
            input.on(Input.EventType.TOUCH_MOVE,this.onTouchMove,this);
                
            newRotation.z+=this.revolveSpeed*deltaTime;
            this.TowerCtrl_Btns_Player.setRotationFromEuler(newRotation);

            towerItem.z=-newRotation.z;

            this.towerArray[0].setRotationFromEuler(towerItem);
            this.towerArray[1].setRotationFromEuler(towerItem);
            this.towerArray[2].setRotationFromEuler(towerItem);
            this.towerArray[3].setRotationFromEuler(towerItem);
            this.towerArray[4].setRotationFromEuler(towerItem);
        }else{
            input.off(Input.EventType.TOUCH_MOVE,this.onTouchMove,this);
            this.node.setPosition(0,-100,0);        //还原至初始位置

            newRotation.z=0;
            towerItem.z=0;
            this.TowerCtrl_Btns_Player.setRotationFromEuler(newRotation);

            this.towerArray[0].setRotationFromEuler(towerItem);
            this.towerArray[1].setRotationFromEuler(towerItem);
            this.towerArray[2].setRotationFromEuler(towerItem);
            this.towerArray[3].setRotationFromEuler(towerItem);
            this.towerArray[4].setRotationFromEuler(towerItem);
        }

    }

}


